<template>
  <div class="app-container">
    <!-- 添加或修改客户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="渠道" prop="channel">
          <el-input v-model="form.channel" placeholder="请输入渠道" />
        </el-form-item>
        <el-form-item label="别名" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入别名" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="form.sex">
            <el-radio-button label="男"></el-radio-button>
            <el-radio-button label="女"></el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="星级" prop="level">
          <el-rate v-model="form.level"></el-rate>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" filterable placeholder="请选择">
            <el-option
              v-for="dict in dict.type.custom_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分组" prop="groupType">
          <el-select v-model="form.groupType" filterable  placeholder="请选择">
            <el-option
              v-for="dict in dict.type.custom_group_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="其他资质" prop="otherCert">
          <el-input v-model="form.otherCert" type="textarea" rows='5' placeholder="请输入其他资质" />
        </el-form-item>
        <el-form-item label="附属信息" prop="sateInfo">
          <el-checkbox-group v-model="checkedInfos" >
            <el-checkbox v-for="city in sateInfos" :label="city" :key="city">{{city}}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-show='show'>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCustom, addCustom, updateCustom } from "@/api/custom/custom";
const cityOptions = ['本地人', '车', '房', '信用卡','寿险保单','公积金','社保','打发工资','营业执照'];
export default {
  name: "CustomModal",
  dicts: ['custom_status','custom_group_type'],
  data() {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {
      },
      show:false,
      checkedInfos: [],
      sateInfos:cityOptions,
      // 表单校验
      rules: {
        name: [
          { required: true, message: "姓名不能为空！", trigger: "blur" }
        ],
        channel:[
          { required: true, message: "渠道不能为空！", trigger: "blur" }
        ]
      }
    };
  },
  created() {
  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.checkedInfos=[];
      this.show = true;
      this.form = {
        id: null,
        no: null,
        name: null,
        phone: null,
        nickName: null,
        sex: null,
        channel: null,
        level: null,
        status: "1",
        groupType: "1",
        otherCert: null,
        sateInfo: null,
        remark: null,
        createBy: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户";
      this.show = true;
    },
    /** 修改按钮操作 */
    handleUpdate(id) {
      this.reset();
      this.show = true;
      getCustom(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户";
        this.checkedInfos = JSON.parse(response.data.sateInfo);
      });
    },
    handleDetail(id){
      this.reset();
      getCustom(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看客户";
        this.checkedInfos = JSON.parse(response.data.sateInfo);
        this.show = false;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.sateInfo = JSON.stringify(this.checkedInfos) ;
          console.log(this.form);
          if (this.form.id != null) {
            updateCustom(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('refresh')
            });
          } else {
            addCustom(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit('refresh')
            });
          }
        }
      });
    },
  }
};
</script>
